package com.jf.blog_jf04.utils;


import java.util.HashMap;
import java.util.Map;

public class R extends HashMap<String, Object> {
    private static final String CODE = "code";
    private static final String MSG = "msg";
    private static final String DATA = "data";
    private static final int SUCCESS_CODE = 200;
    private static final int ERROR_CODE = 500;
    private static final String SUCCESS_MSG = "success";

    private R(int code, String msg, Object data) {
        super.put(CODE, code);
        super.put(MSG, msg);
        if (data != null) {
            super.put(DATA, data);
        }
    }

    public static R success(int code, String msg, Object data) {
        return new R(code, msg, data);
    }

    public static R success(Object data) {
        return success(SUCCESS_CODE, SUCCESS_MSG, data);
    }

    public static R success() {
        return success(SUCCESS_CODE, SUCCESS_MSG, null);
    }

    public static R error(int code, String msg) {
        return new R(code, msg, null);
    }

    public static R error(String msg) {
        return error(ERROR_CODE, msg);
    }

    public R put(String key, Object value) {
        Map<String, Object> map = new HashMap<>();
        map.put(key, value);
        super.put(DATA, map);
        return this;
    }
}
